﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class SearchMaintainRecordDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bindproduct();
            }
        }

        private void Bindproduct()
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }
            if (!product.UpgradeForm.IsLoaded)
            {
                product.UpgradeForm.Load();
            }
            if (!product.SparePartApplyForm.IsLoaded)
            {
                product.SparePartApplyForm.Load();
            }
            if (!product.ReturnToFactoryForm.IsLoaded)
            {
                product.ReturnToFactoryForm.Load();
            }
            if (!product.ApplicationForm.GatheringReference.IsLoaded)
            {
                product.ApplicationForm.GatheringReference.Load();
            }
            if (!product.ApplicationForm.UserReference.IsLoaded)
            {
                product.ApplicationForm.UserReference.Load();
            }

            rmaLabel.Text = product.ApplicationForm.ApplicationCode;
            Label1.Text = product.ApplicationForm.User.FullName;
            Label2.Text = product.ProductType.ProductTypeName;
            Label3.Text = product.ProductSerialNumber;
            Label4.Text = product.ProductRepairType;
            Label5.Text = product.ProductProblemDescription;
            Label6.Text = product.ApplicationForm.ReceiveDateTime.HasValue ? product.ApplicationForm.ReceiveDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            Label7.Text = product.ApplicationForm.DistributeDateTime.HasValue ? product.ApplicationForm.DistributeDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            Label8.Text = product.QuotationDateTime.HasValue ? product.QuotationDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            if (product.UpgradeForm.Count > 0)
            {
                Label9.Text = product.UpgradeForm.FirstOrDefault().ApplyTime.Value.ToString("yyyy-MM-dd");
            }
            if (product.SparePartApplyForm.Count > 0)
            {
                Label10.Text = product.SparePartApplyForm.FirstOrDefault().ApplyTime.Value.ToString("yyyy-MM-dd");
            }
            if (product.ReturnToFactoryForm.Count > 0)
            {
                Label11.Text = product.ReturnToFactoryForm.FirstOrDefault().ApplyDate.Value.ToString("yyyy-MM-dd");
            }
            if (product.RequisitionFormReference.IsLoaded)
            {
                product.RequisitionFormReference.Load();
            }

            Label12.Text = product.ConfirmQuotationDateTime.HasValue ? product.ConfirmQuotationDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            Label13.Text = product.RepairCompleteDateTime.HasValue ? product.RepairCompleteDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            if (product.ApplicationForm.Gathering != null)
            {
                Label14.Text = product.ApplicationForm.Gathering.PayDateTime.Value.ToString("yyyy-MM-dd");
            }
            if (product.RequisitionForm != null)
            {
                Label15.Text = product.RequisitionForm.RequisitionDatetime.Value.ToString("yyyy-MM-dd");
            }
            Label16.Text = product.ClientReceiveDateTime.HasValue ? product.ClientReceiveDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            Label17.Text = product.RepairAccessory;
            Label18.Text = product.ReasonToProblem;
            Label19.Text = product.Cost.HasValue ? product.Cost.Value.ToString() : "0";
            Label20.Text = product.ReasonOfCost;
        }
    }
}
